/**
 * 
 */
package com.sip.comadapter.executor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sip.comadapter.common.BaseExecutor;
import com.sip.comadapter.executor.common.Executor;
import com.sip.comserver.communication.CommunicationMessage;
import com.sip.comserver.communication.utils.CommonUtils;

/**
 * GetEligibleOffersExecutor
 */
public class GetEligibleOffersExecutor extends BaseExecutor implements Executor {

    private static final String SELLER_KEY = "Seller";

    private static final String SCOPE_KEY = "scope";

    private static final String USER_ID_KEY = "userId";

    public CommunicationMessage execute(CommunicationMessage message) {

        final CommunicationMessage newMessage = new CommunicationMessage();
        String apiCall = getApiCall(message);
        System.out.println("============>" + apiCall);
        Map<String, String> inputParameterMap = null;
        String userId = null;
        String scope = null;
        String seller = null;
        String responseMessageBody = null;
        DummyResopnseObject resopnseObject = null;
        
        if (apiCall.equals("getEligibleOffers")) {

            inputParameterMap = getInputParameters(message);
            userId = inputParameterMap.get(USER_ID_KEY);
            scope = inputParameterMap.get(SCOPE_KEY);
            seller = inputParameterMap.get(SELLER_KEY);

            // -------TODO Connect to the DEV API Call and get the result---------------
            // Following is a sample Mock Response.
            resopnseObject = getEligibleOffer(userId, scope);

            List<String> offerList = resopnseObject.getElibileOfferList();
            String offerListString = "";
            for (String offer : offerList) {
                offerListString += " " + offer;
            }

            responseMessageBody = "userId:" + userId + "|SellerName:" + seller + "|scope:" + scope + "|Result:" + offerListString;

        } else {
            responseMessageBody = "Configuration Issue." + apiCall + " Executor is not Available";
        }

        // Set the data to Response message
        String inputMessageHeader = message.getMessageHeader();
        newMessage.setMessageHeader(inputMessageHeader);
        newMessage.setMessageBody(responseMessageBody);

        // convert the response to XML
        final Object obj = parseObjectToXML(resopnseObject);

        // Set the data to Response message
        newMessage.setObject(obj);

        return newMessage;
    }

    /**
	 * 
	 */
    private Map<String, String> getInputParameters(final CommunicationMessage message) {

        Map<String, String> inputparmMap = new HashMap<String, String>();

        String inputMessageValueString = message.getMessageBody();
        String sellerValue = "";
        String scopeValue = "";
        String[] data = null;

        data = inputMessageValueString.split("\\|");
        if (null != data) {
            String sellerNameText = data[1];
            String scopeText = data[2];

            try {
                if (data.length > 1) {
                    sellerValue = sellerNameText.substring(data[1].indexOf(":") + 1);
                }
                if (data.length > 2) {
                    scopeValue = scopeText.substring(data[2].indexOf(":") + 1);
                }
            } catch (Exception e) {

                e.printStackTrace();
            }

        }

        // TODO Add input parameter capturing logic here.
        int randomInt = (int) (100000.0 * Math.random());
        // add them in a map.
        inputparmMap.put(USER_ID_KEY, "SAM_" + randomInt);
        inputparmMap.put(SCOPE_KEY, scopeValue);
        inputparmMap.put(SELLER_KEY, sellerValue);

        return inputparmMap;
    }

    /**
     * @param userId
     * @param scope
     * @return
     */
    private DummyResopnseObject getEligibleOffer(final String userId, final String scope) {

        DummyResopnseObject resopnseObject = new DummyResopnseObject();
        return resopnseObject;

    }

    /**
     * Dummy Inner Class
     */
    private class DummyResopnseObject {

        List<String> elegibleOfferList = new ArrayList<String>();

        DummyResopnseObject() {

            elegibleOfferList.add("VX755");
            elegibleOfferList.add("VX850");

        }

        List<String> getElibileOfferList() {

            return elegibleOfferList;
        }

    }

    private String getApiCall(final CommunicationMessage message) {

        String apiCall = "";
        String inputMessageValueString = message.getMessageBody();

        if (inputMessageValueString.contains("API:")) {
            String apiCallString = inputMessageValueString.substring(inputMessageValueString.indexOf(("API:")));

            try {
                apiCall = CommonUtils.find(apiCallString, "API");

            } catch (Exception e) {
                System.out.println("exception-" + e.toString());

            }

        }
        return apiCall;
    }
}
